package com.cangxuan.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cangxuan.app.R;
import com.cangxuan.app.entity.Goods;
import com.cangxuan.app.entity.MarketGoods;
import com.cangxuan.app.ui.collection.CollectionDetailActivity;
import com.cangxuan.app.utils.HttpUtils;

import java.io.File;
import java.util.List;

public class MarketGoodsAdapter extends BaseAdapter {

    private List<MarketGoods> goodsList;
    private Context context;
    private Handler handler = new Handler();

    public MarketGoodsAdapter(List<MarketGoods> goodsList,Context context) {
        this.goodsList = goodsList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int i) {
        return goodsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return goodsList.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        // 声明视图持有类实例
        ViewHolder viewHolder;
        if (view == null) {
            // 实例view对象
            view = LayoutInflater.from(context).inflate(R.layout.fragment_market_grid_item, viewGroup, false);
            // 当ListView第一次获取项的视图时
            viewHolder = new ViewHolder();
            viewHolder.txtGoodsName = view.findViewById(R.id.goods_item_name);
            viewHolder.textPrice = view.findViewById(R.id.textPrice);
            viewHolder.textCirculation = view.findViewById(R.id.textCirculation);
            viewHolder.goodsImg = view.findViewById(R.id.goods_img);
            // 将viewHolder设置在view的tag属性中，用于下一次getView时，即可从view的tag属性中获取。
            view.setTag(viewHolder);
        } else {
            // 当ListView后面再获取项的视图时
            viewHolder = (ViewHolder) view.getTag();
        }
        // 先取出对应下标的Goods
        MarketGoods goods = goodsList.get(i);
        // 给viewHolder中的控件绑定数据
        viewHolder.txtGoodsName.setText(goods.getName());
        viewHolder.textPrice.setText(String.valueOf(goods.getPrice()));
        viewHolder.textCirculation.setText(String.valueOf(goods.getCount()));
        //图片文件路径，格式：/data/data/com.teacher.hong/upload/goods/1.jpg
        String goodsImagePath = "/data/data/" + context.getPackageName() + goods.getImage();
        Log.i("下标为" + i + "的图片文件路径", goodsImagePath);
        File imageFile = new File(goodsImagePath);
        if (imageFile.exists()) {
            //图片已下载过，已存在
            viewHolder.goodsImg.setImageBitmap(BitmapFactory.decodeFile(goodsImagePath));
        } else {
            //图片未下载过，进行下载
            Thread thread = new Thread(() -> {
                boolean result = HttpUtils.download(goods.getImage(), goodsImagePath);
                if (result) {
                    handler.post(() -> {
                        viewHolder.goodsImg.setImageBitmap(BitmapFactory.decodeFile(goodsImagePath));
                    });
                }
            });
            thread.start();
        }
        view.setOnClickListener(view1 -> {
            Intent intent = new Intent();
            intent.setClass(context, CollectionDetailActivity.class);
            intent.putExtra("isHome", false);
            intent.putExtra("goodsId", goodsList.get(i).getId());
            context.startActivity(intent);
        });
        return view;
    }

    /**
     * 视图持有类，
     * 该类的属性会包含ListView的项里面需要动态设置值的控件
     */
    public class ViewHolder {
        TextView txtGoodsName;
        TextView textPrice;
        TextView textCirculation;
        ImageView goodsImg;
    }
}
